<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="行业名称" prop="industryId">
        <template>
          <el-select v-model="dataForm.industryId" placeholder="请选择">
            <el-option
              v-for="item in dataForm.industryList"
              :key="item.industryId"
              :label="item.industryName"
              :value="item.industryId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <!--<el-form-item label="职业编号" prop="professionNum">-->
        <!--<el-input v-model="dataForm.professionNum" placeholder="职业编号"></el-input>-->
      <!--</el-form-item>-->
      <el-form-item label="职业名称" prop="professionName">
        <el-input v-model="dataForm.professionName" placeholder="职业名称"></el-input>
      </el-form-item>
      <el-form-item label="职业简介" prop="remark">
        <el-input v-model="dataForm.remark"  type="textarea" placeholder="职业简介"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <template>
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          professionId: 23,
          industryId: '',
          professionNum: '',
          professionName: '',
          remark: '',
          status: 1,
          createUser: '',
          createTime: '',
          updateUser: '',
          updateTime: '',
          industryList: [],
          industryName: ''
        },
        dataRule: {
          industryId: [
            {required: true, message: '行业名称不能为空', trigger: 'blur'}
          ],
          professionNum: [
            {required: true, message: '职业编号不能为空', trigger: 'blur'}
          ],
          professionName: [
            {required: true, message: '职业名称 - 如:java工程师不能为空', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '职业简介不能为空', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur'}
          ],
          createUser: [
            {required: true, message: '创建人不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: true, message: '创建时间不能为空', trigger: 'blur'}
          ],
          updateUser: [
            {required: true, message: '修改人不能为空', trigger: 'blur'}
          ],
          updateTime: [
            {required: true, message: '修改时间不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.professionId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // 获取行业信息
          this.$http({
            url: this.$http.adornUrl(`/operate/industry/totalList`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.professionName = ''
              this.dataForm.industryList = data.list
            }
          })
          if (this.dataForm.professionId) {
            this.$http({
              url: this.$http.adornUrl(`/operate/profession/info/${this.dataForm.professionId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.industryId = data.profession.industryId
                this.dataForm.professionNum = data.profession.professionNum
                this.dataForm.professionName = data.profession.professionName
                this.dataForm.remark = data.profession.remark
                this.dataForm.status = data.profession.status
                this.dataForm.createUser = data.profession.createUser
                this.dataForm.createTime = data.profession.createTime
                this.dataForm.updateUser = data.profession.updateUser
                this.dataForm.updateTime = data.profession.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/operate/profession/${!this.dataForm.professionId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'professionId': this.dataForm.professionId || undefined,
                'industryId': this.dataForm.industryId,
                'professionNum': this.dataForm.professionNum,
                'professionName': this.dataForm.professionName,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'createUser': this.dataForm.createUser,
                'createTime': this.dataForm.createTime,
                'updateUser': this.dataForm.updateUser,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
